#coding=utf-8
from datetime import date

from django.core import serializers
from django.core.paginator import Paginator
from django.http import HttpResponse
from django.http import JsonResponse
from django.shortcuts import render, redirect

# Create your views here.
from booktest.models import BookInfo, AreaInfo


def index(request):
    #查询所有图书
    booklist=BookInfo.books.all()
    #字典数据
    context={'booklist':booklist}
    return render(request,'booktest/index.html',context)

# 增加图书
# def create(request):
#     #创建书对象
#     book=BookInfo()
#     #设置数据
#     book.btitle='流星蝴蝶剑'
#     book.bpub_date=date(1995,12,30)
#     #保数据
#     book.save()
#     #转向到首页
#     return redirect('/')
# 点击跳转至新增图书页面
def create(request):
    return render(request,'booktest/create.html')

# 将编辑的表单信息提交给index
def add(request):
    # 通过request的post属性创建对象，提取用户(表单提交的)数据
    dict = request.POST
    book=BookInfo()
    book.btitle=dict.get('btitle')
    # da=str(dict.get('bpub_date'))
    # print(da[0:4])
    # book.bpub_date=date(int(da[0:4]),int(da[5:7]),int(da[8:10]))

    book.bpub_date=dict.get('bpub_date')
    # print(book.bpub_date)
    book.bread=dict.get('bread')
    # print(book.bread)
    book.bcomment=dict.get('bcomment')

    book.save()
    return redirect('/')

# 删除图书
def delete(request,id):
    #根据id查询图书，从url传递过来的都是字符串
    book=BookInfo.books.get(pk=int(id))
    book.isDelete=True
    book.save()
    #转向到首页
    return redirect('/')

# 查看图书
def retrieve(request,id):
    book = BookInfo.books.get(pk=int(id))
    # 获取该图书的所有英雄
    herolist=book.heroinfo_set.all()
    context={'herolist':herolist,'btitle':book.btitle}
    return render(request,'booktest/herolist.html',context)

# 点击跳转至编辑图书页面
def edit(request,id):
    #根据id查询图书
    book=BookInfo.books.get(id=id)
    id=book.id
    btitle=book.btitle
    bpub_data=book.bpub_date
    bread=book.bread
    bcomment=book.bcomment
    context={'id':id,'btitle':btitle,'bpub_date':bpub_data,'bread':bread,'bcomment':bcomment}
    return render(request, 'booktest/edit.html', context)


#将表单提交的数据赋值给对应id的图书
def update(request):
    # 通过request的post属性创建对象，提取用户(表单提交的)数据
    dict = request.POST
    id=dict.get('id')

    book = BookInfo.books.get(id=id)
    book.btitle = dict.get('btitle')
    book.bpub_date = dict.get('bpub_date')
    book.bread = dict.get('bread')
    book.bcomment = dict.get('bcomment')
    book.save()
    return redirect('/')




def formmethod(request):
    return render(request, 'booktest/formmethod.html')

def formgetmethod(request):
    return HttpResponse('get')

def formpostmethod(request):
    return HttpResponse('post')

def querydict(request):
    return render(request,'booktest/querydict.html')

def showquerydict(request):
    # 通过request的post属性创建对象，提取用户(表单提交的)数据
    dict=request.POST
    uname=dict.get('uname')#提取用户姓名
    upwd=dict.get('upwd')#提取用户密码
    ugender=dict.get('ugender')#提取用户性别
    uhobby=dict.getlist('uhobby')#提取用户爱好
    context={'uname':uname,'upwd':upwd,'ugender':ugender,'uhobby':uhobby}
    return render(request, 'booktest/showquerydict.html',context)



def page(request):
    return render(request,'booktest/page.html')

#pageIndex：当前要显示的页码;pageSize:每页显示的条数

def page_test(request,pageIndex,pageSize):
    #查询所有的地区信息
    list1 = AreaInfo.objects.filter(aParent__isnull=True)
    #获取总条数
    totalCount=list1.count()
    #每页显示的条数
    pageSize=int(pageSize)
    #总页数
    totalPage= totalCount/pageSize if totalCount%pageSize == 0 else totalCount/pageSize+1
    #将地区信息按每页显示的条数进行分页
    p = Paginator(list1, pageSize)
    #通过url匹配的参数都是字符串类型，转换成int类型
    pageIndex = int(pageIndex)
    #获取第pageIndex页的数据
    list2 = p.page(pageIndex)
    #构建字典数据
    response_data = {}
    #返回结果
    response_data['result'] = 'Success'
    response_data['pageIndex'] = pageIndex
    response_data['pageSize'] = pageSize
    response_data['totalPage'] = totalPage
    response_data['totalCount'] = totalCount
    #返回信息(通过django.core 包下的serializers进行json序列化)
    response_data['message'] = serializers.serialize('json', list2)
    return JsonResponse(response_data)

def area(request):
    return render(request, 'booktest/area.html')

#获取省信息
def get_pro(request):
    list=AreaInfo.objects.filter(aParent__isnull= True)
    # 构建字典数据
    response_data = {}
    # 返回结果
    response_data['result'] = 'Success'
    # 返回信息
    response_data['message'] = serializers.serialize('json', list)
    return JsonResponse(response_data)